package com.wanli.agent.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.MerchantDetailsBean;
import com.wanli.agent.bean.MerchantShowBean;
import com.wanli.agent.bean.UserInfoBean;
import com.wanli.agent.homepage.adapter.MerchantDetailsListAdapter;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.homepage.model.IHomePageModel;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.widget.AppTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 商户管理
 */
public class MerchantDetailsActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_rate_value)
    TextView tvRateValue;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private String agent_id;
    private IHomePageModel homePageModel = new HomePageModelImpl();
    private MerchantDetailsListAdapter listAdapter;
    private List<MerchantDetailsBean.DataBean.StoreListBean> beanList;
    private MerchantDetailsBean merchantDetailsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchant_details);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        agent_id = getIntent().getStringExtra("agent_id");
        initData();
    }


    public void initData() {
        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        int id = userInfoBean.getData().getId();


        beanList = new ArrayList<>();
        listAdapter = new MerchantDetailsListAdapter(beanList,id);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnClickMyTextView(new MerchantDetailsListAdapter.onClickMyTextView() {
            @Override
            public void myTextViewClick(String store_no) {

                requestStoreSendSignSms(store_no);

            }
        });
    }

    private void requestStoreSendSignSms(String store_no) {
        homePageModel.requestStoreSendSignSms(store_no, new DataCallBack<MerchantShowBean>() {
            @Override
            public void onSuccessful(MerchantShowBean bean) {
                requestMerchantShow();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestMerchantShow();
    }

    private void requestMerchantShow() {
        homePageModel.requestMerchantShow(agent_id, new DataCallBack<MerchantDetailsBean>() {
            @Override
            public void onSuccessful(MerchantDetailsBean bean) {
                merchantDetailsBean = bean;
                tvName.setText(bean.getData().getName());
                tvRateValue.setText(bean.getData().getRate());
                beanList.clear();
                beanList.addAll(bean.getData().getStore_list());
                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        if (merchantDetailsBean == null) {
            return;
        }
        Intent intent = new Intent(mActivity, StoreDataActivity.class);
        intent.putExtra("merchant_id", merchantDetailsBean.getData().getId() + "");
        startActivity(intent);
    }
}
